<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>登录</title>
  <link rel="stylesheet" href="./bootstrap/bootstrap.min.css" />
  <link rel="stylesheet" href="./bootstrap/bootstrap-icons.css" />
  <link rel="stylesheet" href="./css/common.css" />
  <style>
    .form-control:focus {
      border-color: #e0e6ed;
      box-shadow: none;
    }

    .form-control,
    .form-group,
    .input-group {
      transition: none !important;
    }

    .text-blue {
      color: #499FEE;
    }

    .text-blue:hover {
      color: #499FEE;
    }
  </style>
</head>

<body class="application application-offset">
  <div class="container-fluid container-application">
    <div class="main-content position-relative">
      <!-- Page content -->
      <div class="page-content">
        <div class="min-vh-100 py-5 d-flex align-items-center">
          <div class="w-100">
            <div class="row justify-content-center">
              <div class="col-sm-8 col-lg-4">
                <div class="card shadow zindex-100 mb-0">
                  <div class="card-body px-md-5 py-5">
                    <div class="mb-5">
                      <h6 class="h3">传智教育-登录</h6>
                    </div>
                    <span class="clearfix"></span>
                    <form role="form" autocomplete="off">
                      <div class="form-group">
                        <label class="form-control-label">账号</label>
                        <div class="input-group input-group-merge">
                          <div class="input-group-prepend">
                            <span class="input-group-text"><i class="bi bi-person"></i></span>
                          </div>
                          <input name="username" type="text" class="form-control" id="input-email"
                            placeholder="请输入账号" />
                        </div>
                      </div>
                      <div class="form-group mb-4">
                        <div class="d-flex align-items-center justify-content-between">
                          <div>
                            <label class="form-control-label">密码</label>
                          </div>
                          <div class="mb-2">
                            <a href="javascript:;"
                              class="small text-muted text-underline--dashed border-primary">忘记密码</a>
                          </div>
                        </div>
                        <div class="input-group input-group-merge">
                          <div class="input-group-prepend">
                            <span class="input-group-text"><i class="bi bi-key"></i></span>
                          </div>
                          <input name="password" type="password" class="form-control" id="input-password"
                            placeholder="请输入密码" />
                          <div class="input-group-append">
                            <span class="input-group-text">
                              <a href="javascript:;" data-toggle="password-text" data-target="#input-password">
                                <i class="bi bi-eye-fill text-blue"></i>
                              </a>
                            </span>
                          </div>
                        </div>
                      </div>
                      <div class="mt-4">
                        <button id="btn-login" type="button" class="btn btn-sm btn-info btn-icon btn-blue rounded-pill">
                          <span class="btn-inner--text">登录</span>
                          <span class="btn-inner--icon"><i class="bi bi-arrow-right"></i></span>
                        </button>
                      </div>
                    </form>
                  </div>
                  <div class="card-footer px-md-5">
                    <small>没有注册？</small> <a href="./register.html" class="small font-weight-bold text-blue">创建账号</a>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- toast -->
  <div class="position-fixed top-0 start-50 pt-4" style="z-index: 999; transform: translateX(-50%)">
    <div id="myToast" class="toast bg-rgba">
      <div class="toast-body">提示消息</div>
    </div>
  </div>
  <script src="./bootstrap/bootstrap.min.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script src="./lib/axios.js"></script>
  <script src="./js/common.js"></script>
  <script>
    // 登录功能
    //获取登录提交按钮并绑定点击事件
    document.querySelector("#btn-login").addEventListener("click", function () {
      // 用serialize来接收用户在表单提交的数据并以json对象的形式赋值给data
      const data = serialize(document.querySelector("form"), { hash: true })
      console.log(data);
      // 利用对象解构将 data中的值赋给默认值为""的变量username和password
      const { username = '', password = '' } = data
      // 利用正则表达式来筛选用户输入内容,并令其只满足与有字母和数字组成且位数为2~30
      const pandaunUser = /^\w{2,30}$/ //\w 1~9a~z
      // 判断接收的数据是否符合正则表达式要求,如果不符合则弹出提示框
      if (!pandaunUser.test(username)) return show("账号由2~30位字母或数字组成")

      const pandaunPsd = /^\w{6,30}$/ //\w 1~9a~z
      if (!pandaunPsd.test(password)) return show("密码由6~30位字母或数字组成")
      // 发送ajax请求将用户输入的账号密码上传至服务器并进行比对验证
      axios({
        url: "/login",
        method: "post",
        data: {
          username,
          password
        }
      }).then((res) => {
        console.log(res);
        // 输入正确账号密码后跳转网页,并弹出提示框提醒用户登录成功
        location.href="./index.html"
        // 将用户的账号名和token信息保存至本地存储
        localStorage.setItem("cms-user",res.data.username)
        localStorage.setItem("cms-tkn",res.data.token)
        console.log(res.data.token);
        show("登录成功!")
      }).catch(err=>{
        // 当账号密码输入错误时弹出提示框提醒用户账号密码输入错误
        console.dir(err);
        show(err.response.data.message)
      })
    })
  </script>
</body>

</html>